package com.yaowk.web.system.controller;

import com.yaowk.api.system.Config;
import com.yaowk.api.system.model.Menu;
import com.yaowk.api.system.model.Permission;
import com.yaowk.api.system.service.IMenuService;
import com.yaowk.api.system.service.IPermissionService;
import com.yaowk.api.system.service.IRoleService;
import com.yaowk.web.common.model.Transfer;
import com.yaowk.web.common.model.Tree;
import com.yaowk.web.system.utils.MenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/system/menu")
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IRoleService roleService;

    @GetMapping()
    @ApiOperation("菜单列表")
    public Tree<Menu,Integer> list() {
        List<Menu> menus = menuService.findAll();
        Tree<Menu,Integer> tree = new Tree<>();
        Menu menu = menuService.findById(Config.MENU_ROOT_ID);
        tree.setValue(menu);
        MenuUtil.findMenuChildren(menus, tree);
        return tree;
    }

    @GetMapping("/{id}")
    @ApiOperation("菜单详情")
    public Menu index(@PathVariable Integer id) {
        Menu menu = menuService.findById(id);
        return menu;
    }

    @PostMapping()
    @ApiOperation("保存菜单")
    public Menu save(Menu menu) {
        menu = menuService.save(menu);
        return menu;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除菜单")
    public void delete(@PathVariable Integer id) {
        menuService.deleteById(id);
    }

    @GetMapping("/permission/{id}")
    @ApiOperation("权限列表")
    public Transfer<Permission, Integer> permissionList(@PathVariable Integer id) {
        // 查询所有权限和这个菜单的权限
        List<Permission> permissions = permissionService.findPermissionsByMenuId(id);
        List<Permission> allPermissions = permissionService.findAll();

        // 封装成transfer对象
        Transfer<Permission, Integer> transfer = new Transfer<>();
        Set<Integer> checkedIds = transfer.getCheckedKeys();
        Set<Transfer<Permission, Integer>.TransferData> permissionSet = transfer.getData();
        // 封装选中的权限
        for (Permission permission : permissions) {
            checkedIds.add(permission.getId());
        }
        // 封装权限格式
        for (Permission permission : allPermissions) {
            Transfer<Permission, Integer>.TransferData transferData = transfer.new TransferData();
            transferData.setKey(permission.getId());
            transferData.setLabel(permission.getPermission());
            transferData.setValue(permission);
            permissionSet.add(transferData);
        }

        return transfer;
    }

    @PostMapping("/permission")
    @ApiOperation("权限编辑")
    public void permissionEdit(Integer menuId, @RequestParam(required = false) List<Integer> permissionId) {
        menuService.permissionEdit(menuId, permissionId);
    }
}
